/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	Dotnetnuke PA module 
	Copyright (c) 2004, Oliver Hine (oliver.hine@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


************************************************************************************************/


using Microsoft.VisualBasic;
using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI.WebControls;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.Web;

namespace Votations.NSurvey.WebAdmin.UserControls
{
	public enum MenuItems 
	{
		NewSurvey, SurveyOptions, SurveySecurity, SurveyStats, SurveyBuilder, 
		TheLibrary, ResultsReport, Mailing, ASPNetCode, UsersManager}

	public class HeaderControl : PageBase
	{
		protected System.Web.UI.WebControls.HyperLink FieldReportHyperlink;
		protected System.Web.UI.WebControls.HyperLink Hyperlink1;
		protected System.Web.UI.WebControls.HyperLink Hyperlink2;
		protected System.Web.UI.WebControls.DataList SubMenuDatalist;
		protected System.Web.UI.WebControls.PlaceHolder subMenuPlaceHolder;
		protected System.Web.UI.WebControls.DataList MainMenuDataList;
	
		protected override void OnInit(EventArgs e)
		{
			base.OnInit (e);
			InitializeComponent();
		}


		private void InitializeComponent()
		{
			this.MainMenuDataList.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler(this.MainMenuDataList_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			LocalizePage();
		}
	
		private void LocalizePage()
		{
			_menuIndex = (Request["menuindex"]==null) ? -1 : Convert.ToInt32(Request["menuindex"]);

			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.CreateSurvey))
				menuItems.Add(GetPageResource("NewSurveyHyperLink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.CreateSurveyLink, SurveyId, (int)NavigationMenuItems.NewSurvey));
			
			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessSurveySettings))
				menuItems.Add(GetPageResource("SurveyOptionsHyperlink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveyOptionsLink, SurveyId, (int)NavigationMenuItems.SurveyOptions));

			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessSecuritySettings))
				menuItems.Add(GetPageResource("SurveySecurityHyperLink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveySecurityLink, SurveyId, (int)NavigationMenuItems.SurveySecurity));
			
			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessStats))
				menuItems.Add(GetPageResource("SurveyStatsHyperlink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveyStats, SurveyId, (int)NavigationMenuItems.SurveyStats));
			
			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessFormBuilder))
				menuItems.Add(GetPageResource("SurveyBuilderHyperlink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveyContentBuilderLink, SurveyId, (int)NavigationMenuItems.SurveyBuilder));
			
			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessLibrary))
				menuItems.Add(GetPageResource("TheLibraryHyperlink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.LibraryDirectoryHyperLink, SurveyId, (int)NavigationMenuItems.TheLibrary));

			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessReports))
				menuItems.Add(GetPageResource("ResultsReportHyperlink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.ResultsReportHyperlink, SurveyId, (int)NavigationMenuItems.ResultsReport));
			
			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessMailing))
				menuItems.Add(GetPageResource("MailingHyperlink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveyMailing, SurveyId, (int)NavigationMenuItems.Mailing));
			
			if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessASPNetCode))	
				menuItems.Add(GetPageResource("ASPNETCodeHyperlink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.ASPNETCode, SurveyId, (int)NavigationMenuItems.ASPNetCode));

			if (!IsSingleUserMode(false) && 
				(NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessUserManager)))	
				menuItems.Add(GetPageResource("UsersManagerHyperlink"), 
					string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.UsersManagerHyperLink, SurveyId, (int)NavigationMenuItems.UsersManager));


			MainMenuDataList.DataSource = menuItems;
			
			if (_selectedIndex != -1)
			{
				MainMenuDataList.SelectedIndex = _selectedIndex;
			}			

			MainMenuDataList.DataBind();

			BindSubMenu(_menuIndex);

		}

		private void BindSubMenu(int menuIndex)
		{

			switch (menuIndex)
			{
					// Settings submenu
				case (int)NavigationMenuItems.SurveyOptions :
				{
					if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessSurveySettings))	
					{
						subMenuItems.Add(GetPageResource("SurveySettingsHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveyOptionsLink, SurveyId, menuIndex));
						if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessPrivacySettings))	
							subMenuItems.Add(GetPageResource("PrivacySettingsHyperlink"), 
								string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveyPrivacyLink, SurveyId, menuIndex));
					}
					break;
				}

					// Survey builder submenu
				case (int)NavigationMenuItems.SurveyBuilder :
				{
					if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessFormBuilder))
					{
						subMenuItems.Add(GetPageResource("SurveyBuilderHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveyContentBuilderLink, SurveyId, menuIndex));
						subMenuItems.Add(GetPageResource("AnswerEditorHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.AnswerEditorHyperLink, SurveyId, menuIndex));
						if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessAnswerTypeEditor))	
							subMenuItems.Add(GetPageResource("TypeBuilderHyperlink"), 
								string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.TypeEditor, SurveyId, menuIndex));
					}
					break;
				}
					// library submenu
				case (int)NavigationMenuItems.TheLibrary :
				{
					if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessLibrary))
					{
						subMenuItems.Add(GetPageResource("LocalLibraryHyperLink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.LibraryDirectoryHyperLink, SurveyId, menuIndex));
					
						int libraryId = 
							Information.IsNumeric(Request["LibraryId"]) ? int.Parse(Request["LibraryId"]) : -1;
						if (libraryId != -1)
						{
							LibraryData library = new Libraries().GetLibraryById(libraryId);
							if (library.Libraries.Rows.Count > 0)
							{
								subMenuItems.Add(library.Libraries[0].LibraryName, 
									string.Format("{0}&surveyid={1}&libraryid={2}&menuindex={3}", UINavigator.LibraryTemplatesHyperLink, SurveyId, libraryId, menuIndex));
							}
						}
					}
					break;

				}
					// Results submenu
				case (int)NavigationMenuItems.ResultsReport :
				{

					if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessReports))	
						subMenuItems.Add(GetPageResource("GraphicResultsReportHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.ResultsReportHyperlink, SurveyId, menuIndex));
					if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessCrossTab))	
						subMenuItems.Add(GetPageResource("TextFieldsResultsReportHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.FieldsReportHyperlink, SurveyId, menuIndex));
					if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessCrossTab))	
						subMenuItems.Add(GetPageResource("CrossTabResultsReportHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.CrossTabHyperLink, SurveyId, menuIndex));
					if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessExport))	
						subMenuItems.Add(GetPageResource("ExportDataHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.ExportData, SurveyId, menuIndex));
					break;
				}	
					// Mailing submenu
				case (int)NavigationMenuItems.Mailing :
				{

					if (NSurveyUser.Identity.IsAdmin || NSurveyUser.HasRight(NSurveyRights.AccessMailing))
					{
						subMenuItems.Add(GetPageResource("MailingHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.SurveyMailing, SurveyId, menuIndex));
						subMenuItems.Add(GetPageResource("MailingStatusHyperlink"), 
							string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.MailingStatus, SurveyId, menuIndex));
					}
					break;
				}
				
					
					// User manager submenu
				case (int)NavigationMenuItems.UsersManager :
				{

					subMenuItems.Add(GetPageResource("UsersManagerHyperlink"), 
						string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.UsersManagerHyperLink, SurveyId, menuIndex));
					subMenuItems.Add(GetPageResource("RolesManagerHyperlink"), 
						string.Format("{0}&surveyid={1}&menuindex={2}", UINavigator.RolesManagerHyperLink, SurveyId, menuIndex));
					break;
				}
				default :
				{
					subMenuPlaceHolder.Visible = false;
					break;
				}

			}

			SubMenuDatalist.DataSource = subMenuItems;
			SubMenuDatalist.DataBind();
		}

		public new int SurveyId
		{
			get { return (ViewState["SurveyID"]==null) ? -1 : int.Parse(ViewState["SurveyID"].ToString()); }
			set { ViewState["SurveyID"] = value; }
		}

		protected NameValueCollection 
			menuItems = new NameValueCollection(),
			subMenuItems = new NameValueCollection();

		int _selectedIndex = -1,
			_menuIndex = -1;

		private void MainMenuDataList_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
		{
			if (e.Item.ItemType == ListItemType.Item ||
				e.Item.ItemType == ListItemType.AlternatingItem)
			{
				string menuTitle = e.Item.DataItem.ToString();
				bool changeColor = false;
				if (menuTitle == GetPageResource("NewSurveyHyperLink") &&
					_menuIndex == (int)NavigationMenuItems.NewSurvey)
				{
					changeColor = true;
				}
				else if (menuTitle == GetPageResource("SurveyOptionsHyperlink") &&
					_menuIndex == (int)NavigationMenuItems.SurveyOptions)
				{
					changeColor = true;
				}
				else if (menuTitle == GetPageResource("SurveySecurityHyperLink") &&
					_menuIndex == (int)NavigationMenuItems.SurveySecurity)
				{
					changeColor = true;
				}		
				else if (menuTitle == GetPageResource("SurveyStatsHyperlink") &&
					_menuIndex == (int)NavigationMenuItems.SurveyStats)
				{
					changeColor = true;
				}
				else if (menuTitle == GetPageResource("SurveyBuilderHyperlink") &&
					_menuIndex == (int)NavigationMenuItems.SurveyBuilder)
				{
					changeColor = true;
				}
				else if (menuTitle == GetPageResource("TheLibraryHyperlink") &&
					_menuIndex == (int)NavigationMenuItems.TheLibrary)
				{
					changeColor = true;
				}
				else if (menuTitle == GetPageResource("ResultsReportHyperlink") &&
					_menuIndex == (int)NavigationMenuItems.ResultsReport)
				{
					changeColor = true;
				}
				else if (menuTitle == GetPageResource("MailingHyperlink") &&
					_menuIndex == (int)NavigationMenuItems.Mailing)
				{
					changeColor = true;
				}
				else if (menuTitle == GetPageResource("ASPNETCodeHyperlink") &&
					_menuIndex == (int)NavigationMenuItems.ASPNetCode)
				{
					changeColor = true;
				}
				else if (menuTitle == GetPageResource("UsersManagerHyperlink") &&
					_menuIndex == (int)NavigationMenuItems.UsersManager)
				{
					changeColor = true;
				}

				if (changeColor)
				{
					((HyperLink)e.Item.Controls[1]).ForeColor = System.Drawing.Color.White;
				}
			}
		}
	}
}
